package com.benmei.weike.web.teacher;
import com.benmei.weike.common.Constants;
import com.benmei.weike.common.Lang;
import com.benmei.weike.common.Page;
import com.benmei.weike.common.filter.RequestPage;
import com.benmei.weike.dto.mcCourse.McCoursewareDto;
import com.benmei.weike.dto.teacher.McTeaScheduleDto;
import com.benmei.weike.dto.teacher.McTeacherCourseDto;
import com.benmei.weike.exception.ClientException;
import com.benmei.weike.service.McCourseService;
import com.benmei.weike.service.McCourseTeacherService;
import com.benmei.weike.service.common.MemcachedService;
import com.benmei.weike.util.JsonUtil;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by chenhong on 2018/10/17 14:02
 * Description : 老师--我的
 */
@Controller
public class McTeacherCourseController {
    public static final Logger logger= LoggerFactory.getLogger(McTeacherCourseController.class);
    @Autowired
    private MemcachedService memcachedService;
    @Autowired
    private McCourseTeacherService mcCourseTeacherService;
    @Autowired
    private McCourseService mcCourseService;
    /**
     *--课程列表  0(课程未完成)或者1(课程已完成)
     * @param reqPage
     * @param token
     * @return
     */
    @RequestMapping(value="/teacher/my/teatoken/mcCourse",method = {RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo getCourse(@RequestBody RequestPage reqPage,
                             @RequestHeader(value="token") String token){
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(reqPage));

            //course_state只能为1(课程已完成)或者0(课程未完成)
            int course_state = reqPage.getCourse_state();
            if (course_state != 1 && course_state != 0) {
                throw new ClientException("course_state参数错误 "+course_state);
            }
            //获取老师id
            TdNtTeacherResult teacher = (TdNtTeacherResult) memcachedService.get(token);
            Integer teacherId = teacher.getTea_id();
            reqPage.setObj(teacherId);
            //查询老师课程
            List<McTeacherCourseDto> list = mcCourseTeacherService.getMcCourses(reqPage);

            //设置数据
            Page page = new Page(reqPage.getCurrentPage(), reqPage.getPageSize(), reqPage.getTotalRecord());
            page.setData(list);
            RetInfo retInfo = RetInfo.getSuccessInfo();
            retInfo.setObj(page);
            //打印出参
            logger.info(Constants.prefix_out+JsonUtil.toJson(retInfo));
            return retInfo;
        }catch (ClientException e){ //客户端异常
            RetInfo retInfo = RetInfo.getClientErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }catch (Exception e) {
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.EN);
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * 查询课程中的课列表信息
     * @param reqPage
     * @return
     */
    @RequestMapping(value="/teacher/my/teatoken/mcCourse/schedule",method = {RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo getCourseScheduleList(@RequestBody RequestPage reqPage){
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(reqPage));

            //查询课列表
            com.github.pagehelper.Page<McTeaScheduleDto> page =  mcCourseTeacherService.getMcScheduleList(reqPage);

            //设置返回数据
            Map<String,Object> resultMap= new LinkedHashMap();
            resultMap.put("totalPage",page.getPages());
            resultMap.put("data",page);

            //返回客户端数据
            RetInfo retInfo = RetInfo.getSuccessInfo(resultMap);
            //打印出参
            logger.info(Constants.prefix_out+JsonUtil.toJson(retInfo));

            return retInfo;
        }catch (ClientException e){ //客户端异常
            RetInfo retInfo = RetInfo.getClientErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }catch (Exception e) {
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.EN);
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * 获取课件
     * @param mcScheduleId
     * @return
     */
    @RequestMapping(value="/mcCourse/courseware/{mcScheduleId}",method = {RequestMethod.POST,RequestMethod.GET},consumes = "application/json")
    @ResponseBody
    public RetInfo getCoursewares(@PathVariable("mcScheduleId") Integer mcScheduleId){
        try {
            //打印出参
            logger.info(Constants.prefix_in+JsonUtil.toJson(mcScheduleId));

            //获取课件列表
            List<McCoursewareDto> coursewareList=mcCourseService.findCoursewares(mcScheduleId);

            //返回结果
            RetInfo retInfo=RetInfo.getSuccessInfo(coursewareList);
            //打印出参
            logger.info(Constants.prefix_out+JsonUtil.toJson(retInfo));
            return retInfo;
        }catch (Exception e){
            RetInfo retInfo=RetInfo.getServerErrorInfo(Lang.EN);
            logger.error(e.getMessage(),e);
            logger.error(Constants.prefix_out+JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}
